package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"rbook/api/request"
	"rbook/globals"
	"rbook/intermal/model"
	"time"
)

func MessageList(c *gin.Context) {
	param := request.MessageList{}
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	var data []model.Message
	globals.DB.Where("type=?", param.Type).Where("is_read=?", param.IsRead).Find(&data)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "消息展示成功",
		"data": data,
	})
}

func TimeAgo(context *gin.Context) {
	time.AfterFunc(time.Second*5, func() {
		globals.DB.Where("created_at<=?", time.Now().AddDate(0, 0, -30)).Delete(&model.User{})
		fmt.Println("定时任务开启", time.Now().Format("2006-01-01"))
	})
}
